package cn.edu.scau.common.aspect;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;


/**
 * Description: 系统日志切面
 */
@Aspect   // 使用@Aspect注解声明一个切面
@Component
public class LogAspect {

    final static Logger logger = LoggerFactory.getLogger(LogAspect.class);

    @Pointcut("execution(public public * edu.scau.cn.controller.*.*(..))")
    public void Pointcut() {
    }

    // 环绕通知
    @Around("Pointcut()")
    public Object aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
        StringBuilder result = new StringBuilder();
        result.append(" className = " + pjp.getTarget().getClass().getName());
        result.append(" methodName = " + pjp.getSignature().getName());
        result.append(" params = " + JSON.toJSONString(pjp.getArgs(), SerializerFeature.IgnoreNonFieldGetter));
        logger.info("start: " + result.toString());
        // 执行切入点的具体方法
        Object response = pjp.proceed();
        logger.info("end: " + JSON.toJSONString(response));
        return response;

    }

}
